﻿@using MvcContrib.UI.Grid;
@using XRM.Security.DataAccess.Enums;
@using XRM.Security.WebUI.MvcHelpers
@model XRM.Security.WebUI.Models.UserRegister.UserRegisterInfoSystemEditModel
@{
    Layout = null;
}

@if ( Model.InfoSystemAllList.Any() && ( Model.UserRegister.Status == UserStatus.Work ) )
{
<div>
    @using ( Ajax.BeginForm( "GrantInfoSystem", "UserRegister", null, new AjaxOptions { HttpMethod = "Post", OnSuccess = "RefreshPage" }, new { id = "formEditInfoSystem", @class = "form-inline" } ) )
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor( m => m.UserRegisterId )
        
        <div class="form-group">
            @Html.LabelFor( m => m.InfoSystemId )
            @Html.DropDownListFor( m => m.InfoSystemId, new SelectList( Model.InfoSystemAllList, "Id", "InfoSystemName" ), new { @class = "form-control input-400" } )
        </div>
        <div class="form-group">
            <button class="btn btn-primary" type="submit" id="btnGrantInfosystem">Предоставить доступ</button>
        </div>
        <div>
            @Html.ValidationMessageFor( m => m.InfoSystemId )
        </div>
    }
</div>
<br/>
}
<div class="filtered-table">
    @if ( Model.InfoSystemList.Any() )
    {
        <div class="checkbox">
            <label for="@Html.IdFor( m => m.ShowRevoke )">
                @Html.CheckBoxFor( x => x.ShowRevoke )
                Отображать  системы, к которым доступ был закрыт
            </label>
        </div>
        Html.Grid( Model.InfoSystemList ).Columns( column =>
        {
            column.For( item => item.InfoSystem.InfoSystemName ).Named( "Наименование системы" );
            column.For( item => item.GrantDate.ToString( "dd.MM.yyyy" ) ).Named( "Дата предоставления доступа" );
            column.For( item => item.GrantSystemUser.FullName ).Named( "ФИО предоставившего доступ" );
            column.Custom(@<text>
                                    @if ( item.RevokeDate.HasValue )
                                    {
                                        <text>@item.RevokeDate.Value.ToString( "dd.MM.yyyy" )</text>
                                        <i class="fa fa-comment" rel="tooltip" title="@item.RevokeReason" data-placement="top"></i>
                                    }
                              </text>).Named( "Дата запрета доступа" );
            column.For( item => item.RevokeSystemUser == null ? "" : item.RevokeSystemUser.FullName ).Named( "ФИО запретившего доступ" );
            column.Custom(@<text>
                                @if ( !item.RevokeDate.HasValue )
                                {
                                    @Html.DialogFormButton(new DialogButtonOptions()
                                    {
                                        Text = "Запретить доступ",
                                        ContentUrl = Url.Action("RevokeInfoSystemDialog", "UserRegister", new { item.Id }),
                                        Width = "800",
                                        Id = "dlgRevokeInfoSystemDialog" + item.Id,
                                        ButtonId = "btnRevokeInfoSystemDialog" + item.Id,
                                        OnSuccess = "RefreshPage",
                                        Image = "fa-trash-o"
                                    })
                                }
                              </text>)
                      .Named( "<i class='fa fa-trash-o fa-action'></i>" ).Attributes( @class => "single-action" );

        } ).Attributes( @class => "table table-striped table-bordered", id => "table_id" )
        .RowAttributes( item => new MvcContrib.Hash( @class => item.Item.RevokeDate.HasValue ? "inactive hide" : "" ) )
        .Render();
    }
</div>

<script type="text/javascript">
    $("#ShowRevoke").change(function () {
        if ($(this).is(":checked")) {
            $("#table_id .inactive").removeClass("hide");
        }
        else {
            $("#table_id .inactive").addClass("hide");
        }
    });

    $(document).ready(function () {
        @if ( Model.InfoSystemList.Any() )
        {
                <text>
                $('#table_id').dataTable({
                    paging: false,
                    info: false,
                    bFilter: false,
                    bSort: false
                });
                </text>
        }
        $("[rel=tooltip]").tooltip();
        $("#ShowRevoke").change();
    });
</script>
